﻿using System;
using System.IO;
using System.Xml.Linq;

namespace Tequila.Phone.Sharing.Twitpic
{
    /// <summary>
    /// Twitpic response helper utility.
    /// </summary>
    public static class TwitpicResponseUtility
    {
        /// <summary>
        /// Parses response into <see cref="TwitpicResponse">TwitpicResponse</see> object.
        /// </summary>
        /// <param name="xmLresponse"></param>
        /// <returns></returns>
        public static TwitpicResponse ParseResponse(string xmLresponse)
        {
            using (var stringReader = new StringReader(xmLresponse))
            {
                var xDocument = XDocument.Load(stringReader);

                try
                {
                    var rootNode = xDocument.Root;
                    if (rootNode == null)
                    {
                        return null;
                    }

                    try
                    {
                        var mediaId = rootNode.GetElementValue("id");
                        var mediaUrl = rootNode.GetElementValue("url");
                        try
                        {
                            var user = rootNode.Element("user");
                            var userId = 0;
                            if (user != null)
                            {
                                userId = int.Parse(user.GetElementValue("id"));
                            }
                            return new TwitpicResponse(userId, mediaId, mediaUrl);
                        }
                        catch
                        {
                            return new TwitpicResponse(mediaId, mediaUrl);
                        }
                    }
                        //<err code="1001" msg="Invalid twitter username or password" />
                        //errorCode = int.Parse(rootNode.Element("err").GetAttributeValue("code"));
                        //errorMessage = rootNode.Element("err").GetAttributeValue("msg");
                        //return new TwitPicResponse(errorCode, errorMessage);
                    catch
                    {
                        //if (rootNode.GetAttributeValue("status") == "ok") //Method: Upload
                        //{
                        //    mediaId = rootNode.GetElementValue("mediaid");
                        //    mediaUrl = rootNode.GetElementValue("mediaurl");
                        //    return new TwitPicResponse(mediaId, mediaUrl);
                        //}
                        //<err code="1001" msg="Invalid twitter username or password" />
                        var errorCode = int.Parse(rootNode.Element("err").GetAttributeValue("code"));
                        var errorMessage = rootNode.Element("err").GetAttributeValue("msg");
                        return new TwitpicResponse(errorCode, errorMessage);
                    }
                }
                catch (Exception ex)
                {
                    return new TwitpicResponse("XML Parse error: " + ex);
                }
            }
        }
    }
}